/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainFrame.java
 *
 * Created on Feb 11, 2011, 9:56:00 PM
 */
package autotrade.application.gui;

import autotrade.application.AutoTradeLocalData;
import autotrade.core.database.StockInfoDaily;
import autotrade.core.database.User;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import autotrade.core.virtualsystem.AutoTrade;
import autotrade.application.gui.panel.AddOrderPanel;
import autotrade.application.gui.panel.ImportDataPanel;
import autotrade.application.gui.panel.NewUserPanel;
import autotrade.application.gui.panel.SettingPanel;
import autotrade.core.importdata.DailyUpdate;
import java.awt.Color;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JDialog;
import javax.swing.SpinnerDateModel;
import javax.swing.table.DefaultTableModel;
import org.jfree.data.xy.*;
import org.jfree.chart.*;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYBarPainter;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;

/**
 *
 * @author Dinh
 */
public class MainFrame extends javax.swing.JFrame {

    public JDialog newUserDialog;
    public JDialog settingDialog;
    public JDialog importFileDialog;
    public JDialog addOrderDialog;
    
    DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
    Calendar calendar = Calendar.getInstance();
    public static StockInfoDaily currentDateStockInfo;
    public static StockInfoDaily centerDateStockInfo;

    /** Creates new form MainFrame */
    public MainFrame() {
        initComponents();
        initOthers();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainJTabbedPane = new javax.swing.JTabbedPane();
        transactionJPanel = new javax.swing.JPanel();
        chartJPanel = new javax.swing.JPanel();
        symbolJLabel = new javax.swing.JLabel();
        symbolJComboBox = new javax.swing.JComboBox();
        openPriceJLabel = new javax.swing.JLabel();
        openPriceJTextField = new javax.swing.JTextField();
        highPriceJLabel = new javax.swing.JLabel();
        highPriceJTextField = new javax.swing.JTextField();
        lowPriceJLabel = new javax.swing.JLabel();
        lowPriceJTextField = new javax.swing.JTextField();
        closePriceJLabel = new javax.swing.JLabel();
        closePriceJTextField = new javax.swing.JTextField();
        volumeJLabel = new javax.swing.JLabel();
        volumeJTextField = new javax.swing.JTextField();
        final XYDataset priceDataset = createPriceDataset(AutoTradeLocalData.load().getCenter_date().getTime(),AutoTradeLocalData.load().getCurrentDate().getTime(), null, "close");
        final XYDataset volumeDataset = createVolumeDataset(AutoTradeLocalData.load().getCenter_date().getTime(),AutoTradeLocalData.load().getCurrentDate().getTime(), null);
        final JFreeChart chart = createPriceVolumeChart(priceDataset, volumeDataset);
        priceVolumeChartJPanel = new ChartPanel(chart);
        priceTypeJLabel = new javax.swing.JLabel();
        priceTypeJComboBox = new javax.swing.JComboBox();
        numberOfDayJLabel = new javax.swing.JLabel();
        numberOfDayJSpinner = new javax.swing.JSpinner();
        dateJSpinner = new javax.swing.JSpinner();
        dateJLabel = new javax.swing.JLabel();
        AccountInfoJPanel = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        userJComboBox = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        cashRemainJLabel = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        userPortfolioJTable = new javax.swing.JTable();
        jLabel7 = new javax.swing.JLabel();
        gainlossjLabel = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        orderListJTable = new javax.swing.JTable();
        removeOrderJButton = new javax.swing.JButton();
        addOrderJButton = new javax.swing.JButton();
        dateJPanel = new javax.swing.JPanel();
        nextYearJButton = new javax.swing.JButton();
        nextMonthJButton = new javax.swing.JButton();
        nextDayJButton = new javax.swing.JButton();
        currentDateJLabel = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        userManagementJPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        newJButton = new javax.swing.JButton();
        removeJButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        listUserJTable = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        updateDataPanel = new javax.swing.JPanel();
        pickFileJButton = new javax.swing.JButton();
        importJButton = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        filePathJTextPane = new javax.swing.JTextPane();
        jButton4 = new javax.swing.JButton();
        jMenuBar = new javax.swing.JMenuBar();
        editJMenu = new javax.swing.JMenu();
        settingJMenuItem = new javax.swing.JMenuItem();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        symbolJLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        symbolJLabel.setText("Symbol:");

        symbolJComboBox.setEditable(true);
        symbolJComboBox.setModel(new javax.swing.DefaultComboBoxModel(autotrade.core.virtualsystem.AutoTrade.LIST_ALL_JSC_SYMBOL.toArray()));
        JTextField field = (JTextField)symbolJComboBox.getEditor().getEditorComponent();
        field.setText("");
        field.addKeyListener(new ComboKeyHandler(symbolJComboBox));
        symbolJComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                symbolJComboBoxActionPerformed(evt);
            }
        });

        openPriceJLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        openPriceJLabel.setText("Open:");

        openPriceJTextField.setEditable(false);
        openPriceJTextField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        highPriceJLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        highPriceJLabel.setText("High:");

        highPriceJTextField.setEditable(false);
        highPriceJTextField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        lowPriceJLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        lowPriceJLabel.setText("Low:");

        lowPriceJTextField.setEditable(false);
        lowPriceJTextField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        closePriceJLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        closePriceJLabel.setText("Close:");

        closePriceJTextField.setEditable(false);
        closePriceJTextField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        volumeJLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        volumeJLabel.setText("Volume:");

        volumeJTextField.setEditable(false);
        volumeJTextField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        javax.swing.GroupLayout priceVolumeChartJPanelLayout = new javax.swing.GroupLayout(priceVolumeChartJPanel);
        priceVolumeChartJPanel.setLayout(priceVolumeChartJPanelLayout);
        priceVolumeChartJPanelLayout.setHorizontalGroup(
            priceVolumeChartJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 633, Short.MAX_VALUE)
        );
        priceVolumeChartJPanelLayout.setVerticalGroup(
            priceVolumeChartJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 533, Short.MAX_VALUE)
        );

        priceTypeJLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        priceTypeJLabel.setText("Price Type:");

        priceTypeJComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Open", "High", "Low", "Close" }));
        priceTypeJComboBox.setSelectedIndex(3);
        priceTypeJComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                priceTypeJComboBoxActionPerformed(evt);
            }
        });

        numberOfDayJLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        numberOfDayJLabel.setText("Number of day:");

        numberOfDayJSpinner.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(AutoTradeLocalData.load().getNumber_of_day()), Integer.valueOf(0), null, Integer.valueOf(1)));
        numberOfDayJSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                numberOfDayJSpinnerStateChanged(evt);
            }
        });

        Date earliestDate = new Date(AutoTrade.earliestTime);
        Date latestDate = new Date(AutoTrade.latestTime);
        dateJSpinner.setModel(new javax.swing.SpinnerDateModel(AutoTradeLocalData.load().getCenter_date(), earliestDate, latestDate, Calendar.DAY_OF_MONTH));
        dateJSpinner.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        dateJSpinner.setEditor(new JSpinner.DateEditor(dateJSpinner, "MM/dd/yyyy"));
        dateJSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                dateJSpinnerStateChanged(evt);
            }
        });

        dateJLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        dateJLabel.setText("Center Date:");

        javax.swing.GroupLayout chartJPanelLayout = new javax.swing.GroupLayout(chartJPanel);
        chartJPanel.setLayout(chartJPanelLayout);
        chartJPanelLayout.setHorizontalGroup(
            chartJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(chartJPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(chartJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(symbolJLabel)
                    .addComponent(priceTypeJLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(chartJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(symbolJComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(priceTypeJComboBox, 0, 74, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(chartJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(chartJPanelLayout.createSequentialGroup()
                        .addComponent(openPriceJLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(openPriceJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(highPriceJLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(highPriceJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(lowPriceJLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lowPriceJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(closePriceJLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(closePriceJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(volumeJLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(volumeJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(chartJPanelLayout.createSequentialGroup()
                        .addComponent(numberOfDayJLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(numberOfDayJSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(dateJLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(dateJSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(priceVolumeChartJPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        chartJPanelLayout.setVerticalGroup(
            chartJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(chartJPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(chartJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(symbolJLabel)
                    .addComponent(openPriceJLabel)
                    .addComponent(openPriceJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(highPriceJLabel)
                    .addComponent(highPriceJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lowPriceJLabel)
                    .addComponent(lowPriceJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(closePriceJLabel)
                    .addComponent(closePriceJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(volumeJLabel)
                    .addComponent(volumeJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(symbolJComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(chartJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(priceTypeJComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(numberOfDayJLabel)
                    .addComponent(numberOfDayJSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(priceTypeJLabel)
                    .addComponent(dateJSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(dateJLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(priceVolumeChartJPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("User");

        userJComboBox.setModel(new javax.swing.DefaultComboBoxModel(User.LIST_ALL_USER.toArray()));
        userJComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                userJComboBoxActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setText("Cash Remain");

        cashRemainJLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        cashRemainJLabel.setText("1000");

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 10));
        jLabel5.setText("x1000 VND");

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel6.setText("User Portfolio");

        userPortfolioJTable.setModel(new javax.swing.table.DefaultTableModel(

            new Object[][] {
            },
            new String [] {
                "Symbol", "Volume", "Buy Price", "Current Price", "Gain/Loss"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Long.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(userPortfolioJTable);

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel7.setText("Gain/Loss");

        gainlossjLabel.setText("0.0");

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel9.setText("Order List:");

        orderListJTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Type", "Symbol", "Price", "Volume", "Value"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Double.class, java.lang.Long.class, java.lang.Double.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane3.setViewportView(orderListJTable);

        removeOrderJButton.setText("Remove Order");
        removeOrderJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeOrderJButtonActionPerformed(evt);
            }
        });

        addOrderJButton.setText("Add Order");
        addOrderJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addOrderJButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout AccountInfoJPanelLayout = new javax.swing.GroupLayout(AccountInfoJPanel);
        AccountInfoJPanel.setLayout(AccountInfoJPanelLayout);
        AccountInfoJPanelLayout.setHorizontalGroup(
            AccountInfoJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, AccountInfoJPanelLayout.createSequentialGroup()
                .addGroup(AccountInfoJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, AccountInfoJPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 473, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, AccountInfoJPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel6))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, AccountInfoJPanelLayout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addGroup(AccountInfoJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(AccountInfoJPanelLayout.createSequentialGroup()
                                .addGroup(AccountInfoJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel3))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(AccountInfoJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(userJComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, AccountInfoJPanelLayout.createSequentialGroup()
                                        .addComponent(cashRemainJLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel5)))
                                .addGap(18, 18, 18)
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(gainlossjLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(AccountInfoJPanelLayout.createSequentialGroup()
                                .addComponent(jLabel9)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 195, Short.MAX_VALUE)
                                .addComponent(addOrderJButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(removeOrderJButton))))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, AccountInfoJPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 473, Short.MAX_VALUE)))
                .addContainerGap())
        );

        AccountInfoJPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {addOrderJButton, removeOrderJButton});

        AccountInfoJPanelLayout.setVerticalGroup(
            AccountInfoJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(AccountInfoJPanelLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(AccountInfoJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(userJComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(17, 17, 17)
                .addGroup(AccountInfoJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel7)
                    .addComponent(cashRemainJLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(gainlossjLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(AccountInfoJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(removeOrderJButton)
                    .addComponent(addOrderJButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(12, 12, 12)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        nextYearJButton.setText("Next Year");
        nextYearJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextYearJButtonActionPerformed(evt);
            }
        });

        nextMonthJButton.setText("Next Month");
        nextMonthJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextMonthJButtonActionPerformed(evt);
            }
        });

        nextDayJButton.setText("Next Day");
        nextDayJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextDayJButtonActionPerformed(evt);
            }
        });

        currentDateJLabel.setText(dateFormat.format(AutoTradeLocalData.load().getCurrentDate()));

        jLabel12.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel12.setText("Current Date: ");

        javax.swing.GroupLayout dateJPanelLayout = new javax.swing.GroupLayout(dateJPanel);
        dateJPanel.setLayout(dateJPanelLayout);
        dateJPanelLayout.setHorizontalGroup(
            dateJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, dateJPanelLayout.createSequentialGroup()
                .addContainerGap(708, Short.MAX_VALUE)
                .addComponent(jLabel12)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(currentDateJLabel)
                .addGap(18, 18, 18)
                .addComponent(nextDayJButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(nextMonthJButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(nextYearJButton)
                .addContainerGap())
        );
        dateJPanelLayout.setVerticalGroup(
            dateJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(dateJPanelLayout.createSequentialGroup()
                .addGroup(dateJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nextYearJButton)
                    .addComponent(nextMonthJButton)
                    .addComponent(nextDayJButton)
                    .addComponent(currentDateJLabel)
                    .addComponent(jLabel12))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout transactionJPanelLayout = new javax.swing.GroupLayout(transactionJPanel);
        transactionJPanel.setLayout(transactionJPanelLayout);
        transactionJPanelLayout.setHorizontalGroup(
            transactionJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(transactionJPanelLayout.createSequentialGroup()
                .addComponent(AccountInfoJPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(chartJPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(dateJPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        transactionJPanelLayout.setVerticalGroup(
            transactionJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(transactionJPanelLayout.createSequentialGroup()
                .addComponent(dateJPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(transactionJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(chartJPanel, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(AccountInfoJPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(76, 76, 76))
        );

        mainJTabbedPane.addTab("Transaction", transactionJPanel);

        jLabel1.setText("List of existing Users");

        newJButton.setText("New");
        newJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newJButtonActionPerformed(evt);
            }
        });

        removeJButton.setText("Remove");
        removeJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeJButtonActionPerformed(evt);
            }
        });

        Object[][] data = User.getUserTableData();
        listUserJTable.setModel(new javax.swing.table.DefaultTableModel(
            data,
            new String [] {
                "User ID", "User Name", "User Type", "Technical Analysis Method", "Cash Remain", "Active"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Double.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        listUserJTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        jScrollPane1.setViewportView(listUserJTable);
        listUserJTable.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        listUserJTable.getColumnModel().getColumn(0).setPreferredWidth(25);
        listUserJTable.getColumnModel().getColumn(1).setPreferredWidth(35);
        listUserJTable.getColumnModel().getColumn(2).setPreferredWidth(25);
        listUserJTable.getColumnModel().getColumn(3).setPreferredWidth(100);
        listUserJTable.getColumnModel().getColumn(4).setPreferredWidth(100);
        listUserJTable.getColumnModel().getColumn(5).setPreferredWidth(5);

        jButton1.setText("Refresh");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Start");

        jButton3.setText("Stop");

        javax.swing.GroupLayout userManagementJPanelLayout = new javax.swing.GroupLayout(userManagementJPanel);
        userManagementJPanel.setLayout(userManagementJPanelLayout);
        userManagementJPanelLayout.setHorizontalGroup(
            userManagementJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, userManagementJPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(userManagementJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1023, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(userManagementJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(newJButton, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE)
                    .addComponent(removeJButton, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE))
                .addContainerGap())
        );
        userManagementJPanelLayout.setVerticalGroup(
            userManagementJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(userManagementJPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(userManagementJPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(userManagementJPanelLayout.createSequentialGroup()
                        .addComponent(newJButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(removeJButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(208, Short.MAX_VALUE))
        );

        mainJTabbedPane.addTab("User Management", userManagementJPanel);

        pickFileJButton.setText("Pick a file");

        importJButton.setText("Import");

        jScrollPane4.setViewportView(filePathJTextPane);

        jButton4.setText("Force Update Data");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout updateDataPanelLayout = new javax.swing.GroupLayout(updateDataPanel);
        updateDataPanel.setLayout(updateDataPanelLayout);
        updateDataPanelLayout.setHorizontalGroup(
            updateDataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(updateDataPanelLayout.createSequentialGroup()
                .addGroup(updateDataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(updateDataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(updateDataPanelLayout.createSequentialGroup()
                            .addGap(252, 252, 252)
                            .addComponent(importJButton))
                        .addGroup(updateDataPanelLayout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(pickFileJButton)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jScrollPane4)))
                    .addGroup(updateDataPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jButton4)))
                .addContainerGap(829, Short.MAX_VALUE))
        );
        updateDataPanelLayout.setVerticalGroup(
            updateDataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(updateDataPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(updateDataPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pickFileJButton)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addComponent(importJButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton4)
                .addContainerGap(549, Short.MAX_VALUE))
        );

        mainJTabbedPane.addTab("Update Data", updateDataPanel);

        editJMenu.setText("Edit");

        settingJMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        settingJMenuItem.setText("Setting ...");
        settingJMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                settingJMenuItemActionPerformed(evt);
            }
        });
        editJMenu.add(settingJMenuItem);

        jMenuBar.add(editJMenu);

        jMenu1.setText("Import");

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_I, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem1.setText("Import From File");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem2.setText("Force update data");
        jMenu1.add(jMenuItem2);

        jMenuBar.add(jMenu1);

        setJMenuBar(jMenuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainJTabbedPane)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainJTabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 674, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        AutoTradeLocalData.load().saveAutoTradeLocalData();
    }//GEN-LAST:event_formWindowClosing

    private void newJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newJButtonActionPerformed
        newUserDialog = new JDialog();
        NewUserPanel panel = new NewUserPanel(this);
        newUserDialog.add(panel);
        newUserDialog.pack();
        newUserDialog.setVisible(true);
}//GEN-LAST:event_newJButtonActionPerformed

    private void nextDayJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextDayJButtonActionPerformed
        calendar.setTime(AutoTradeLocalData.load().getCurrentDate());
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        if (calendar.getTimeInMillis() > AutoTrade.latestTime) {
            return;
        }

        AutoTradeLocalData.load().setCurrentDate(calendar.getTime());
        currentDateJLabel.setText(dateFormat.format(AutoTradeLocalData.load().getCurrentDate()));

        //update center date
        AutoTradeLocalData.load().setCenter_date(AutoTradeLocalData.load().getCurrentDate());
        SpinnerDateModel dateModel = (SpinnerDateModel) dateJSpinner.getModel();
        dateModel.setEnd(AutoTradeLocalData.load().getCurrentDate());
        dateJSpinner.setValue(AutoTradeLocalData.load().getCurrentDate());

        updatePriceAndVolume();
        updatePriceVolumeChart();

        for (User user : User.LIST_ALL_USER) {
            user.updateUserInfoCurrentDateChange();
        }

        updateUIUser((User)userJComboBox.getSelectedItem());
}//GEN-LAST:event_nextDayJButtonActionPerformed

    private void nextMonthJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextMonthJButtonActionPerformed
        calendar.setTime(AutoTradeLocalData.load().getCurrentDate());
        calendar.add(Calendar.MONTH, 1);
        if (calendar.getTimeInMillis() > AutoTrade.latestTime) {
            return;
        }

        AutoTradeLocalData.load().setCurrentDate(calendar.getTime());
        currentDateJLabel.setText(dateFormat.format(AutoTradeLocalData.load().getCurrentDate()));

        //update center date
        AutoTradeLocalData.load().setCenter_date(AutoTradeLocalData.load().getCurrentDate());
        SpinnerDateModel dateModel = (SpinnerDateModel) dateJSpinner.getModel();
        dateModel.setEnd(AutoTradeLocalData.load().getCurrentDate());
        dateJSpinner.setValue(AutoTradeLocalData.load().getCurrentDate());

        updatePriceAndVolume();
        updatePriceVolumeChart();

        for (User user : User.LIST_ALL_USER) {
            user.updateUserInfoCurrentDateChange();
        }

        updateUIUser((User)userJComboBox.getSelectedItem());
}//GEN-LAST:event_nextMonthJButtonActionPerformed

    private void nextYearJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextYearJButtonActionPerformed
        calendar.setTime(AutoTradeLocalData.load().getCurrentDate());
        calendar.add(Calendar.YEAR, 1);
        if (calendar.getTimeInMillis() > AutoTrade.latestTime) {
            return;
        }
        AutoTradeLocalData.load().setCurrentDate(calendar.getTime());
        currentDateJLabel.setText(dateFormat.format(AutoTradeLocalData.load().getCurrentDate()));

        //update center date
        AutoTradeLocalData.load().setCenter_date(AutoTradeLocalData.load().getCurrentDate());
        SpinnerDateModel dateModel = (SpinnerDateModel) dateJSpinner.getModel();
        dateModel.setEnd(AutoTradeLocalData.load().getCurrentDate());
        dateJSpinner.setValue(AutoTradeLocalData.load().getCurrentDate());

        updatePriceAndVolume();
        updatePriceVolumeChart();

        for (User user : User.LIST_ALL_USER) {
            user.updateUserInfoCurrentDateChange();
        }

        updateUIUser((User)userJComboBox.getSelectedItem());
    }//GEN-LAST:event_nextYearJButtonActionPerformed

    private void dateJSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_dateJSpinnerStateChanged
        Date centerDate = (Date) dateJSpinner.getValue();
        AutoTradeLocalData.load().setCenter_date(centerDate);
        //        ArrayList<String> listJSCSymbol = AutoTradeInfo.getAllJSCSymbolAtThisTime(currentDate);
        //        symbolJComboBox.setModel(new javax.swing.DefaultComboBoxModel(listJSCSymbol.toArray()));
        //        symbolJComboBox.setSelectedItem(symbol);

        updatePriceAndVolume();
        updatePriceVolumeChart();

        ((User)userJComboBox.getSelectedItem()).updateUserInfoCenterDateChange();
        updateUIUser((User)userJComboBox.getSelectedItem());

    }//GEN-LAST:event_dateJSpinnerStateChanged

    private void numberOfDayJSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_numberOfDayJSpinnerStateChanged
        Integer time = (Integer) numberOfDayJSpinner.getValue();
        AutoTradeLocalData.load().setNumber_of_day(time.intValue());

        updatePriceVolumeChart();
}//GEN-LAST:event_numberOfDayJSpinnerStateChanged

    private void priceTypeJComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_priceTypeJComboBoxActionPerformed
        updatePriceVolumeChart();
}//GEN-LAST:event_priceTypeJComboBoxActionPerformed

    private void symbolJComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_symbolJComboBoxActionPerformed
        String symbol = (String) symbolJComboBox.getSelectedItem();
        AutoTradeLocalData.load().setSymbol(symbol);

        updatePriceAndVolume();
        updatePriceVolumeChart();
}//GEN-LAST:event_symbolJComboBoxActionPerformed

    private void settingJMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_settingJMenuItemActionPerformed
        settingDialog = new JDialog();
        SettingPanel panel = new SettingPanel(this);
        settingDialog.add(panel);
        settingDialog.pack();
        settingDialog.setVisible(true);
    }//GEN-LAST:event_settingJMenuItemActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        importFileDialog = new JDialog();
        ImportDataPanel panel = new ImportDataPanel(this);
        importFileDialog.add(panel);
        importFileDialog.pack();
        importFileDialog.setVisible(true);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void removeJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeJButtonActionPerformed
        int removeIndex = listUserJTable.getSelectedRow();
        User.removeUser(User.LIST_ALL_USER.get(removeIndex).getUserID());
        User.LIST_ALL_USER.remove(removeIndex);
        DefaultTableModel model = (DefaultTableModel) listUserJTable.getModel();
        model.removeRow(removeIndex);
        listUserJTable.repaint();

        DefaultComboBoxModel comboModel = (DefaultComboBoxModel) userJComboBox.getModel();
        comboModel.removeElementAt(removeIndex);
        userJComboBox.repaint();
    }//GEN-LAST:event_removeJButtonActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        User.LIST_ALL_USER = User.getAllUsers();
        DefaultTableModel model = (DefaultTableModel) listUserJTable.getModel();
        model.setDataVector(User.getUserTableData(), new String[]{
                    "User ID", "User Name", "User Type", "Technical Analysis Method", "Cash Remain", "Active"
                });
        listUserJTable.repaint();

        userJComboBox.setModel(new DefaultComboBoxModel(User.LIST_ALL_USER.toArray()));
        userJComboBox.repaint();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void userJComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_userJComboBoxActionPerformed
        updateUIUser((User)userJComboBox.getSelectedItem());
    }//GEN-LAST:event_userJComboBoxActionPerformed

    private void addOrderJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addOrderJButtonActionPerformed
        addOrderDialog = new JDialog();
        AddOrderPanel panel = new AddOrderPanel(this);
        addOrderDialog.add(panel);
        addOrderDialog.pack();
        addOrderDialog.setVisible(true);
    }//GEN-LAST:event_addOrderJButtonActionPerformed

    private void removeOrderJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeOrderJButtonActionPerformed
        int removeIndex = orderListJTable.getSelectedRow();
        User selectedUser = (User)userJComboBox.getSelectedItem();
        selectedUser.getListcurrentDateOrders().remove(removeIndex);
        
        DefaultTableModel model = (DefaultTableModel) orderListJTable.getModel();
        model.removeRow(removeIndex);
        orderListJTable.repaint();

    }//GEN-LAST:event_removeOrderJButtonActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        System.out.println("update data");
        DailyUpdate.run();
    }//GEN-LAST:event_jButton4ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                AutoTrade.earliestTime = AutoTrade.getEarliestTimeInDatabase();
                AutoTrade.latestTime = AutoTrade.getLatestTimeInDatabase();
                MainFrame mainFrame = new MainFrame();
                mainFrame.setVisible(true);
                mainFrame.updateUIUser((User)mainFrame.userJComboBox.getSelectedItem());
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel AccountInfoJPanel;
    private javax.swing.JButton addOrderJButton;
    private javax.swing.JLabel cashRemainJLabel;
    private javax.swing.JPanel chartJPanel;
    private javax.swing.JLabel closePriceJLabel;
    private javax.swing.JTextField closePriceJTextField;
    private javax.swing.JLabel currentDateJLabel;
    private javax.swing.JLabel dateJLabel;
    private javax.swing.JPanel dateJPanel;
    private javax.swing.JSpinner dateJSpinner;
    private javax.swing.JMenu editJMenu;
    private javax.swing.JTextPane filePathJTextPane;
    private javax.swing.JLabel gainlossjLabel;
    private javax.swing.JLabel highPriceJLabel;
    private javax.swing.JTextField highPriceJTextField;
    private javax.swing.JButton importJButton;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    public javax.swing.JTable listUserJTable;
    private javax.swing.JLabel lowPriceJLabel;
    private javax.swing.JTextField lowPriceJTextField;
    private javax.swing.JTabbedPane mainJTabbedPane;
    private javax.swing.JButton newJButton;
    private javax.swing.JButton nextDayJButton;
    private javax.swing.JButton nextMonthJButton;
    private javax.swing.JButton nextYearJButton;
    private javax.swing.JLabel numberOfDayJLabel;
    private javax.swing.JSpinner numberOfDayJSpinner;
    private javax.swing.JLabel openPriceJLabel;
    private javax.swing.JTextField openPriceJTextField;
    private javax.swing.JTable orderListJTable;
    private javax.swing.JButton pickFileJButton;
    private javax.swing.JComboBox priceTypeJComboBox;
    private javax.swing.JLabel priceTypeJLabel;
    private javax.swing.JPanel priceVolumeChartJPanel;
    private javax.swing.JButton removeJButton;
    private javax.swing.JButton removeOrderJButton;
    private javax.swing.JMenuItem settingJMenuItem;
    private javax.swing.JComboBox symbolJComboBox;
    private javax.swing.JLabel symbolJLabel;
    private javax.swing.JPanel transactionJPanel;
    private javax.swing.JPanel updateDataPanel;
    public javax.swing.JComboBox userJComboBox;
    private javax.swing.JPanel userManagementJPanel;
    private javax.swing.JTable userPortfolioJTable;
    private javax.swing.JLabel volumeJLabel;
    private javax.swing.JTextField volumeJTextField;
    // End of variables declaration//GEN-END:variables

    private void updatePriceAndVolume() {
        StockInfoDaily stockInfo = StockInfoDaily.getStockInfo((String) symbolJComboBox.getSelectedItem(), AutoTradeLocalData.load().getCenter_date().getTime());

        if (stockInfo == null) {
            openPriceJTextField.setText("");
            highPriceJTextField.setText("");
            lowPriceJTextField.setText("");
            closePriceJTextField.setText("");
            volumeJTextField.setText("");
            return;
        }

        openPriceJTextField.setText(stockInfo.getOpenPrice() + "");
        highPriceJTextField.setText(stockInfo.getHighPrice() + "");
        lowPriceJTextField.setText(stockInfo.getLowPrice() + "");
        closePriceJTextField.setText(stockInfo.getClosePrice() + "");
        volumeJTextField.setText(stockInfo.getVolume() + "");
    }

    private void updatePriceVolumeChart() {
        calendar.setTime(AutoTradeLocalData.load().getCenter_date());
        calendar.add(Calendar.DAY_OF_MONTH, -AutoTradeLocalData.load().getNumber_of_day() / 2);
        long startTime = calendar.getTimeInMillis();
        calendar.add(Calendar.DAY_OF_MONTH, AutoTradeLocalData.load().getNumber_of_day());
        long endTime = calendar.getTimeInMillis();

        if (endTime > AutoTradeLocalData.load().getCurrentDate().getTime()) {
            endTime = AutoTradeLocalData.load().getCurrentDate().getTime();
        }

        final XYDataset priceDataset = createPriceDataset(startTime, endTime, AutoTradeLocalData.load().getSymbol(), (String) priceTypeJComboBox.getSelectedItem());
        final XYDataset volumeDataset = createVolumeDataset(startTime, endTime, AutoTradeLocalData.load().getSymbol());
        final JFreeChart chart = createPriceVolumeChart(priceDataset, volumeDataset);
        ((ChartPanel) priceVolumeChartJPanel).setChart(chart);
    }

    private XYDataset createPriceDataset(long startTime, long endTime, String symbol, String priceType) {
        final TimeSeries priceSeries = AutoTrade.getPriceSeries(startTime, endTime, symbol, priceType);
        final TimeSeriesCollection dataset = new TimeSeriesCollection();
        dataset.addSeries(priceSeries);
        return dataset;
    }

    private XYDataset createVolumeDataset(long startTime, long endTime, String symbol) {
        final TimeSeries volumeSeries = AutoTrade.getVolumeSeries(startTime, endTime, symbol);
        final TimeSeriesCollection dataset = new TimeSeriesCollection();
        dataset.addSeries(volumeSeries);
        return dataset;
    }

    private JFreeChart createPriceVolumeChart(final XYDataset priceDataset, final XYDataset volumeDataset) {
        JFreeChart chart = ChartFactory.createTimeSeriesChart(
                "",
                "Date",
                "Price",
                priceDataset,
                true,
                true,
                false);
        //chart.getXYPlot().getRangeAxis().setLowerBound(0);
        chart.setBackgroundPaint(Color.white);
        XYPlot plot = chart.getXYPlot();

        DateAxis dateAxis = (DateAxis) plot.getDomainAxis();
        dateAxis.setPositiveArrowVisible(true);

        NumberAxis priceRangeAxis = (NumberAxis) plot.getRangeAxis();
        priceRangeAxis.setLowerMargin(0.40); // to leave room for volume bars
        DecimalFormat format = new DecimalFormat("00.00");
        priceRangeAxis.setNumberFormatOverride(format);

        XYItemRenderer priceRenderer = plot.getRenderer();

        priceRenderer.setBaseToolTipGenerator(
                new StandardXYToolTipGenerator(
                StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,
                new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0.00")));



        NumberAxis volumeRangeAxis = new NumberAxis("Volume");
        volumeRangeAxis.setUpperMargin(1.00); // to leave room for price line
        plot.setRangeAxis(1, volumeRangeAxis);
        plot.setDataset(1, new XYBarDataset(volumeDataset, 100000000));
        plot.mapDatasetToRangeAxis(1, 1);
        XYBarRenderer volumeRenderer = new XYBarRenderer(0.20);
        volumeRenderer.setBarPainter(new StandardXYBarPainter());
        volumeRenderer.setShadowVisible(false);
        volumeRenderer.setBaseToolTipGenerator(
                new StandardXYToolTipGenerator(
                StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT,
                new SimpleDateFormat("d-MMM-yyyy"), new DecimalFormat("0,000.00")));
        volumeRenderer.setSeriesPaint(0, Color.GREEN);
        plot.setRenderer(1, volumeRenderer);
        return chart;
    }

    private void initOthers() {
        //updateUIUser((User)userJComboBox.getSelectedItem());
    }

    private void updateUIUser(User selectedUser) {
            if (selectedUser == null)
                return;

            DefaultTableModel model = (DefaultTableModel) orderListJTable.getModel();
            model.setDataVector(selectedUser.getListOrdersTableData(), new String[]{
                        "Type", "Symbol", "Price", "Volume", "Value"
                    });
            orderListJTable.repaint();

            model = (DefaultTableModel) userPortfolioJTable.getModel();
            model.setDataVector(selectedUser.getUserPortfolioTableData(), new String[]{
                        "Symbol", "Volume", "Buy Price", "Current Price", "Gain/Loss"
                    });
            userPortfolioJTable.repaint();

            cashRemainJLabel.setText(selectedUser.getCash_remain() + "");

            double gainloss = ((selectedUser.getCash_remain() + selectedUser.getStock_value())/selectedUser.getInital_cash() - 1)*100;
            gainlossjLabel.setText(gainloss + "");

            boolean check = AutoTradeLocalData.load().getCenter_date().equals(AutoTradeLocalData.load().getCurrentDate());
            addOrderJButton.setVisible(selectedUser.getTypeID() == 1 && check);
            removeOrderJButton.setVisible(selectedUser.getTypeID() == 1 && check);
    }
}
